Ext.QuickTips.init();
Ext.define('Module.sjfx.Syjszs', {
	extend : 'Ext.panel.Panel',
	alias : 'widget.syjszs',
	requires: ['Sh.ux.VideoPlayer'],
	layout:'fit',
	border: false,
	title: '首页警示',
	config:{
		bjs: [],
		bjid: undefined,
		store: undefined,
		vindex: 0,
		isSx:false,   //是否上一组、下一组
		isPause:false //是否暂停
	},
	initComponent : function() {
		var me = this;
		me.store = Ext.create('Ext.data.Store', {
		     fields: [],
		     proxy: {
		         type: 'ajax',
		         url: 'wgSyjsAction/getWgSyjsList.do'
		     },
		     autoLoad: false
		 });
		
		me.on('afterrender',function(){
			me.store.load({callback: function(records){
				me.switchVideo(0);
				me.ruanTask();
			}});
		});
		
		
		var centerPanel =Ext.create('Ext.panel.Panel',{
			region: 'center',
			layout:'column',
			border:false,
			autoScroll: true,
			bodyPadding: '15 30 30 30',
			defaults: {
				border:false,
				bodyPadding: 3,
				columnWidth: 1/3
			},
			items: [{
				id:me.id+'-bt',
				border: 1,
				columnWidth: 1,
				xtype: 'fieldset',
				html: '<span style="font-size: 22px;font-weight: bold;">没有找到警示视频。</span>',
			    style: {
			        borderColor: '#14a296',
			        backgroundColor:'#DBF2EF',
			        'border-radius': '5px'
			    }
			},
			 {
				xtype: 'panel',
				items:[{  
				    xtype: 'box', //或者xtype: 'component',  
				    style: {
				    	'margin-bottom':'-2px'
				    },
				    height: 27, //图片高度  
				    autoEl: {  
				        tag: 'img',    //指定为img标签  
				        src: 'images/syjszs/zc.png'    //指定url路径  
				    }  
				},{
					xtype: 'panel',
					id:me.id+'-zcpalyer',
					layout: 'fit',
					height: 250,
					border: true,
					bodyStyle: {
						 borderColor: '#028b83',
					     backgroundColor:'#DBF2EF',
					     borderStyle: 'solid'
			        }
				},{
			    	xtype:"fieldset",
			    	title:'正常视频描述',
			    	height:110,
					style: {
						borderColor:'#40B0A3',
			            backgroundColor:'#DBF2EF',
				        'border-radius': '5px'
			        },
			        defaults: {
			    		labelWidth: 60
					},
			        border:true,
				        items:[{
							fieldLabel: '视频名称',
							style:"margin-top:13",
							xtype: 'displayfield',
							id:me.id+'-zcvName'
						},{
							fieldLabel: '详情',
							xtype: 'displayfield',
							id:me.id+'-zcnr'
						}]
				}]
				},{
					xtype: 'panel',
					items:[{  
					    xtype: 'box', //或者xtype: 'component',  
					    height: 27, //图片高度  
					    style: {
					    	'margin-bottom':'-2px'
					    },
					    autoEl: {  
					        tag: 'img',    //指定为img标签  
					        src: 'images/syjszs/wg.png'    //指定url路径  
					    }
					},{
						xtype: 'panel',
						id:me.id+'-wgpalyer',
						layout: 'fit',
						height: 250,
						border: true,
						bodyStyle: {
							 borderColor: '#028b83',
						     backgroundColor:'#DBF2EF',
						     borderStyle: 'solid'
				        }
					},{
				    	xtype:"fieldset",
				    	title:'违规视频描述',
				    	height:110,
						style: {
							borderColor:'#40B0A3',
				            backgroundColor:'#DBF2EF',
					        'border-radius': '5px'
				        },
				        defaults: {
				    		labelWidth: 60
						},
				        border:true,
					        items:[{
								fieldLabel: '视频名称',
								xtype: 'displayfield',
								style:"margin-top:13",
								id:me.id+'-wgvName'
							},{
								fieldLabel: '详情',
								xtype: 'displayfield',
								id:me.id+'-wgnr'
							}]
					}]
				},{
					xtype: 'panel',
					items: [{  
					    xtype: 'box', //或者xtype: 'component',  
					    height: 27, //图片高度  
					    style: {
					    	'margin-bottom':'-2px'
					    },
					    autoEl: {  
					        tag: 'img',    //指定为img标签  
					        src: 'images/syjszs/bj.png'    //指定url路径  
					    }
					},{  
					    xtype: 'box',
					    padding: 1,
					    id: 'syjs-wgimg',
					    height:250,
//					    html:'<img width="100%" src="wgVideoAction/getWgImg.do?wgtime=0&videoId='+262+'"/>',
						border: true,
						style: {
						    borderColor: '#028b83',
						    backgroundColor:'#DBF2EF',
						    borderStyle: 'solid'
						}
					},{
				    	xtype:"fieldset",
				    	title:'违规标记信息',
				    	height:110,
						style: {
							borderColor:'#EE7979',
				            backgroundColor:'#FBDFDF',
					        'border-radius': '5px'
				        },
				    	defaults: {
				    		labelWidth: 60
						},
				        border:true,
				        items:[{
							fieldLabel: '违规名称',
							xtype: 'displayfield',
							id: 'syjs-wgmc'
						},{
							fieldLabel: '发生时间',
							xtype: 'displayfield',
							id: 'syjs-wgsj'
						},{
							fieldLabel: '详细描述',
							xtype: 'displayfield',
							id: 'syjs-wgxq'
						}]
					}]
					
				},
				{
				 xtype: 'panel',
				 height:30,
				 columnWidth: 1,
				 defaults: {
					 margin:'0 0 0 10'
				},
				 items: [{
						 xtype:'button',
						 text:'全部播放',
						 iconCls: 'icon-play',
						 handler: function(){
							 if(me.getIsPause()==true){
								 Ext.getCmp(me.id+'-zcpalyer').down('player').play();
								 Ext.getCmp(me.id+'-wgpalyer').down('player').play();
							 }
							 me.setIsPause(false);
							 
						 }
				    },{
						 xtype:'button',
						 text:'全部暂停',
						 iconCls: 'icon-pause',
						 handler: function(){
							 me.setIsPause(true);
							 Ext.getCmp(me.id+'-zcpalyer').down('player').pause();
							 Ext.getCmp(me.id+'-wgpalyer').down('player').pause();
						 }
				    },{
						 xtype:'button',
						 iconCls: 'icon-tostart',
						 text:'上一组',
						 handler: function(){
							 if(me.getVindex() !=0){
								 me.setVindex(me.getVindex()-1);
								me.switchVideo();
							 }else{
								 Ext.Msg.alert('提示', '已经是第一组视频了');
							 }
						 }
				    },{
						 xtype:'button',
						 iconCls: 'icon-toend',
						 text:'下一组',
						 handler: function(){
							 if(me.store.getCount()-1< me.getVindex()+1){
								 Ext.Msg.alert('提示', '已经是最后一组视频了');
							 }else{
								 me.setVindex(me.getVindex()+1);
								 me.switchVideo();
							 }
						 }
				    }
				 ]
				}
		 ]
	});
		
		me.items = [centerPanel];

		me.callParent();
	},
	switchVideo: function(){
		var me=this;
		var index= me.getVindex();
//		index = me.store.getCount() > index ? index : 0;
		var me = this;
		var rec = me.store.getAt(index);
		var t = me.store.getCount();
		if(index >=0 && index < t){
			Ext.getCmp(me.id+'-bt').update({
				html: '<span style="font-size: 22px;font-weight: bold;">警示内容：'+rec.raw.title+'</span>'
			});
			Ext.getCmp(me.id+'-zcpalyer').remove(Ext.getCmp(me.id+'-zcpalyer').down('player'));
			Ext.getCmp(me.id+'-wgpalyer').remove(Ext.getCmp(me.id+'-wgpalyer').down('player'));
			Ext.getCmp(me.id+'-zcvName').setValue(rec.raw.zcvName);
			Ext.getCmp(me.id+'-zcnr').setValue(rec.raw.zcnr);
			Ext.getCmp(me.id+'-wgvName').setValue(rec.raw.wgvName);
			Ext.getCmp(me.id+'-wgnr').setValue(rec.raw.wgnr);
			Ext.getCmp(me.id+'-zcpalyer').add({
				xtype: 'player',
				height:250,
				autoPlay: true,
				hides: ['sign'],
				src: rec.raw.zcSynpath
			});
			
			Ext.getCmp(me.id+'-wgpalyer').add({
				xtype: 'player',
				autoPlay: true,
				hides: ['sign'],
				src: rec.raw.wgSynpath,
				onloadeddata:function(){
					me.updateMd(Ext.getCmp(me.id+'-wgpalyer').down('player'),rec.raw.wgvideoId);
				}
			});
		}
		
	},
	ruanTask: function(){
		var me = this;
		Ext.TaskManager.start({//定时器	
			interval: 500,
			run: function(){
				try {
					/*var p1 = Ext.getCmp(me.id+'-zcpalyer').down('player').isPlay();
					var p2 = Ext.getCmp(me.id+'-wgpalyer').down('player').isPlay();
					
					if((!p1 && !p2 && me.getIsPause() == false) || me.getIsSx()==true){//表示 me.getIsSx()==true是否点击过上一组下一组
//						if(me.getIsSx()==false){
//							me.setVindex(me.getVindex()+1);
//						}
						var index = me.getVindex();
						index = me.store.getCount() > index ? index : 0;
						me.switchVideo(index);
						me.setIsSx(false);
					}*/
					
					var p2 = Ext.getCmp(me.id+'-wgpalyer').down('player');
					if(p2){
						var time = p2.getCurrentTime();
						var bjs = me.getBjs();
						for ( var i = bjs.length-1; i >= 0; i--) {
							var bj = bjs[i];
							if(bj.wgtime < time){
								if(me.bjid != bj.id){
									Ext.getCmp('syjs-wgimg').update({
										html: '<img width="100%" height="100%" src="wgVideoAction/getWgImg.do?wgtime='+bj.wgtime+'&videoId='+bj.videoId+'"/>'
									});
									Ext.getCmp('syjs-wgmc').setValue(bj.title);
									Ext.getCmp('syjs-wgsj').setValue(me.formatSeconds(bj.wgtime));
									Ext.getCmp('syjs-wgxq').setValue(bj.describe);
									me.bjid = bj.id;
								}
								
								break;
							}
						}
					}
					
				} catch (e) {
				}
			}
		});
	},
	updateMd: function(c,videoId){//c:player、vindex:视频id
		var me = this;
		$('div[md]',c.el.dom).remove();
		var prbar = c.down('progressbar');
		$(prbar.el.dom).append("<div md style='filter: alpha(opacity=50); opacity:0.5;postion:relative;width:100%;height:100%;'></div>");
		Ext.Ajax.request({
		    url: 'wgVideoAction/getWgBj.do',
		    params: {videoId:videoId},
		    success: function(response){
		        var text = response.responseText;
		        var data = Ext.decode(text);
		        me.setBjs(data);
		        for ( var key in data) {
		        	me.renderMd(c,data[key]);
				}
		    }
		});
	},
	renderMd: function(c,data){
		var me=this;
		var time = data.wgtime,text=data.text;
		var p = c;
		var prbar = p.down('progressbar');
		var left = time*(prbar.getWidth()/p.getAllTime());
		c.goToTime(parseInt(time));
		var md = $('<div></div>').css('display','inline-block').css('position','absolute').css('opacity',100).css('filter','alpha(opacity=100)')
		.css('background','red').css('height','100%').css('cursor','pointer').css('margin-left',left).width(2)
		.data('data',data).click(function(e){
		});
		$('div[md=]',c.el.dom).append(md);
		Ext.QuickTips.register({
			target: md[0],
			title: '描点,'+data.title,
			text: text,//具体要提示的东西
			width: 200,
			dismissDelay: 100000
		});
	},
	formatSeconds : function(value){//将秒转换为时间
		var theTime = parseInt(value);// 秒
		var theTime1 = 0;// 分
		var theTime2 = 0;// 小时
	    if(theTime > 60) {
	        theTime1 = parseInt(theTime/60);
	        theTime = parseInt(theTime%60);
	            if(theTime1 > 60) {
	            theTime2 = parseInt(theTime1/60);
	            theTime1 = parseInt(theTime1%60);
	            }
	    }
        var result = ""+parseInt(theTime);
        if(theTime1 > 0) {
        	result = ""+parseInt(theTime1)+":"+result;
        }
        if(theTime2 > 0) {
        	result = ""+parseInt(theTime2)+":"+result;
        }
        var r= result.indexOf(":");
        if(r==-1){
        	result="0:"+result;
        }
        return result;
    }
});
